﻿// ***********************************************************************
// Assembly         : INNOVACALL.Framework.Common
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="JQClass.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Script.Serialization;

namespace INNOVACALL.Framework.Common.POCOT4.MVCViewJSObjects
{
    /// <summary>
    /// Class JQClass
    /// </summary>
    public class JQClass
    {
        /// <summary>
        /// The arg
        /// </summary>
        private string arg;
        /// <summary>
        /// The s assembly name
        /// </summary>
        private string sAssemblyName;
        /// <summary>
        /// The assem
        /// </summary>
        private Assembly assem;
        /// <summary>
        /// The types
        /// </summary>
        private Type[] types;

        /// <summary>
        /// Gets the types.
        /// </summary>
        /// <returns>Type[][].</returns>
        public Type[] GetTypes()
        {
            return types;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="JQClass"/> class.
        /// </summary>
        /// <param name="sAssemblyName">Name of the s assembly.</param>
        public JQClass(string sAssemblyName)
        {
            assem = Assembly.LoadFrom(sAssemblyName);
            types = assem.GetTypes();
        }

        /// <summary>
        /// Creates the instance.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>Object.</returns>
        public Object CreateInstance(Type type)
        {
            Object myObject = Activator.CreateInstance(type);
            return myObject;
        }

        /// <summary>
        /// Obtients the list types meta.
        /// </summary>
        /// <param name="ListNom">The list nom.</param>
        /// <returns>List{Type}.</returns>
        public List<Type> ObtientListTypesMeta(List<string> ListNom)
        {
            List<Type> listMetaTypes = new List<Type>();
            foreach (string nom in ListNom)
            {
                listMetaTypes.Add(types.ToList().Find(p => p.Name == nom));
            }
            return listMetaTypes;
        }

        /// <summary>
        /// Obtients the list chaine JS.
        /// </summary>
        /// <param name="lstName">Name of the LST.</param>
        /// <returns>List{System.String}.</returns>
        public List<string> ObtientListChaineJS(List<string> lstName)
        {
	        List<Type> lstTypes = new List<Type>();
	        lstTypes = this.ObtientListTypesMeta(lstName);
	
	        List<string> lstObjetJS = new List<string>();
	        foreach(Type typeCourant in lstTypes)
	        {
		        string jqclassMetaData = typeCourant.Name;
		        object metaData = this.CreateInstance(typeCourant);

		        JavaScriptSerializer serializer = new JavaScriptSerializer();
	            serializer.MaxJsonLength = Int32.MaxValue;
	        
		        string objetjson = serializer.Serialize(metaData).ToString();

		        string chaineJS = "var " + jqclassMetaData + " = " + objetjson +";";
		        lstObjetJS.Add(chaineJS);
	        }
            return lstObjetJS;
        }
    }
}
